package com.ctgu.j08.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctgu.j08.common.enums.ResultEnum;
import com.ctgu.j08.mapper.DepartmentMapper;
import com.ctgu.j08.mapper.FactoryMapper;
import com.ctgu.j08.service.DepartmentService;
import com.ctgu.j08.service.FactoryService;
import com.ctgu.j08.utils.ResultUtil;
import com.ctgu.j08.vo.Department;
import com.ctgu.j08.vo.Factory;
import com.ctgu.j08.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version 1.0
 * @author: Saiya Wang
 * @date: 2021/5/28 21:23
 */
@Service
public class FactoryServiceImpl extends ServiceImpl<FactoryMapper, Factory> implements FactoryService {
    @Autowired
    private FactoryMapper factoryMapper;


    @Override
    public Factory getById(int facId) {
        Factory factory = factoryMapper.selectByPrimaryKey(facId);
        return factory;
    }

    @Override
    public Factory getByfacName(String facName) {
        Factory factory = factoryMapper.selectByfacName(facName);
        return factory;
    }

    @Override
    public Result editFactory(Factory factory) {
        int isEdit = factoryMapper.updateByPrimaryKey(factory);
        if(isEdit != 0)
        {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }
        else
        {
            System.out.println("更新失败"+isEdit);
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result addFactory(Factory factory) {
        boolean isAdd = this.save(factory);
        if(isAdd != true)
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
        else
        {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }
    }

    @Override
    public Result delFactoryById(Integer facId) {
        int isDel = factoryMapper.deleteByPrimaryKey(facId);
        if(isDel!=0)
        {
            System.out.println("删除成功："+facId);
            return ResultUtil.success(ResultEnum.SUCCESS);
        }else
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result getAllFactoryInfo() {
        List<Factory> factories = factoryMapper.getAllFactoryInfo();
        if(factories == null)
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }else
        {
            System.out.println("部门列表："+factories);
            return ResultUtil.success(factories);
        }
    }

    @Override
    public Result searchFactoryByName(String facName) {
        Factory factory = factoryMapper.selectByfacName(facName);
        if(factory != null)
        {
            System.out.println("搜索到的用户"+factory);
            return ResultUtil.success(factory);
        }
        else
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }
}
